#!/usr/bin/tclsh

proc printUsage {} {
    puts "\nUsage: xptoiptc.tclsh \[file|directory\] ?-flag?...\n"

    foreach {flag description} [getFlagsAndDescriptions] {
        puts [format "    %-*s  %s" 12 ${flag} ${description}]
    }
}

proc getFlagsAndDescriptions {} {
    lappend flagList -help {Print this message.}
    lappend flagList -verbose {Print out actions.}
    lappend flagList -recurs {Resurs directories.}

    return $flagList
}

proc getFlags {} {
    foreach {flag description} [getFlagsAndDescriptions] {
        lappend flagList $flag
    }
    return $flagList
}

proc processDirectory {path {verbose false} {recurs false}} {
    foreach fileOrDirectory [lsort [glob -nocomplain $path/*]] {
        if {[file isdirectory $fileOrDirectory] && [string eq true $recurs]} {
           processDirectory $fileOrDirectory $verbose $recurs
           continue
        }
        
        processFile $fileOrDirectory $verbose
    }
}

proc processFile {file {verbose false}} {
    set command [list exiftool -EXIF:XPTitle -EXIF:XPKeywords]
    lappend command -EXIF:XPComment $file
   
    if {[string eq true $verbose]} {
        puts "\nR: ${command}"
    }

    if {[catch {set results [eval exec $command]} err]} {
        puts $err
        return
    }

    set command [list exiftool -overwrite_original]

    foreach line [split $results \n] {
        set parts [split $line ":"]
        set field [join [string trim [lindex $parts 0]] ""]
        set value [string trim [lindex $parts 1]]
        
        switch -exact -- $field {
            XPKeywords {
                lappend command -IPTC:Keywords=

                foreach keyword [split $value ","] {
                    if {[string length [set keyword [string trim $keyword]]]} {
                        lappend command -IPTC:Keywords+=${keyword}
                    }
                }
            }
            XPTitle {
                catch {set value [join $value]}
                lappend command -IPTC:ObjectName=${value}
            }
            XPComment {
                catch {set value [join $value]}
                append command -IPTC:Caption-Abstract=${value}
            }
        }
    }

    lappend command $file

    if {[string eq true $verbose]} {
        puts "W: ${command}"
    }

    if {[catch {eval exec $command} err]} {
        puts "*** ${err} ***"
    }
}

set verbose false
set recurs false
set path ""

for {set x 0} {$x < $argc} {incr x} {
    if {[string match "-*" [lindex $argv $x]]} {
        if {[lsearch -exact [getFlags] [lindex $argv $x]] == -1} {
            printUsage
            return
        }
    }

    if {[string eq -help [lindex $argv $x]]} {
        return [printUsage]
    }

    if {[string eq -verbose [lindex $argv $x]]} {
        set verbose true
        continue
    }

    if {[string eq -recurs [lindex $argv $x]]} {
        set recurs true
        continue
    }

    set path [string trim [lindex $argv $x]]
}

if {![string length $path]} {
    printUsage
    return
} 

if {![file isdirectory $path]} {
    processFile $path $verbose
    return
}

processDirectory $path $verbose $recurs
